<template>
<el-container>
  <el-header style="height: auto;">
    <el-form>
      <el-row>
        <el-col :xl="{ span: 6 }" :lg="{ span: 6 }">
          <el-form-item label="业务号码区间" label-width="110px">
            <el-col :xl="{ span: 11 }" :lg="{ span: 11 }">
              <el-input size="small" style="width: 100%;" v-model.trim="cardNumberStart" disabled="true"></el-input>
            </el-col>
            <el-col :xl="{ span: 2 }" :lg="{ span: 2 }" style="text-align: center;">-</el-col>
            <el-col :xl="{ span: 11 }" :lg="{ span: 11 }">
              <el-input size="small" style="width: 100%;" v-model="cardNumberEnd" disabled="true"></el-input>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :xl="{ span: 3}" :lg="{ span: 3}">
        <el-form-item>
          <el-button size="small" @click="showBatchSetPackage">批量设置套餐</el-button>
        </el-form-item>
      </el-col>
    </el-form>
  </el-header>
  <el-main style="height: auto;">
    <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-loading.fullscreen="tableLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.2)" :row-key="getRowKeys">
      <el-table-column type="selection" :reserve-selection="true" align="center"></el-table-column>
      <el-table-column prop="headName" label="套餐组名称" align="center"></el-table-column>
      <!-- <el-table-column prop="operatorBranch" label="上游公司" align="center"></el-table-column>
      <el-table-column prop="dictSetMealGroupTypeName" label="套餐大类类型" align="center"></el-table-column>
      <el-table-column prop="setMealCount" label="套餐个数" align="center"></el-table-column>
      <el-table-column prop="dictIsUsedName" label="是否启用" align="center"></el-table-column>
     -->
    </el-table>
    <div class="block"><span class="demonstration"></span>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10" layout="prev, pager, next, jumper" :page-count="maxPage" :hide-on-single-page="value" background></el-pagination>
    </div>
  </el-main>
  <el-dialog class="delete" top="33vh" :visible.sync="operation.setPackage" :append-to-body="true" width="30%"><span style="display: flex; justify-content: center;">是否设置套餐？</span><span class="dialog-footer" slot="footer">
      <el-button @click="operation.setPackage = false" size="small">取 消</el-button>
      <el-button type="primary" size="small" @click="isSubmitSetPackage" v-preventReClick>确 定</el-button></span></el-dialog>
</el-container>
</template>

<script>
import { getbalanceHeadList,  setBalanceRoad } from '@/api/getData'

export default {
  name: 'sb',
  data() {
    return {
      cardNumberStart: '',
      cardNumberEnd: '',
      value: false,
      operation: {
        setPackage: false,
      },
      maxPage: 1,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      tableLoading: false,
      mealName: '',
      dictMealType: '',
      officialPrice: '',
      dictTerOfValidityName: '',
      mealSpec: '',
      dictIsTerOfValiditySuper: '',
      insertTime: '',
      id: '',
      cardNumber: '',
      errorList: [],
      successList: [],
      cardList: [],
      tableChecked: [],
      setMealList: {},
      errorPackageList: [],
      errorPackageStr: ''
    }
  },
  created() {
    //判断上个页面过来的查询数据
    let a = this.$route.query.start;
    let b = this.$route.query.end;
    if(a&&b){
      this.cardNumberStart =a;
      this.cardNumberEnd = b;
    }
    this.initData();
  },
  methods: {
    getRowKeys(row) {
      return row.id
    },
    async initData() {
      const returnData = await getbalanceHeadList({
        // mealName: this.mealName,
        page: {
          current: this.currentPage + '',
          size: this.pageSize + '',
          ascs: this.ascs,
          descs: this.descs
        }
      },
      {
        obj: this,
        key: 'tableLoading;'
      })
      this.tableData = []
      if (returnData._result) {
        this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
        // 数据回显
        returnData.data.records.map(item => {
          const data = {}
          data.id = item.id;
          data.oemId = item.oemId;
          data.headName = item.headName;
          this.tableData.push(data)
        })
        this.tableLoading = false
      }
    },
    // 設置套餐
    async isSubmitSetPackage() {
      this.$message({ message: '正在设置中', type: 'warning' })
      this.operation.setPackage = false
      this.setMealList = this.tableChecked.map(item => {
        return item.id
      })
      const res = await setBalanceRoad({
        cardNumberStart: this.cardNumberStart,
        cardNumberEnd: this.cardNumberEnd,
        balanceHeadId: this.setMealList[0]
      },{pagekey: this.pagekey})
      if (res._result) {
          this.$message({ message: '设置成功！', type: 'success' })
          this.operation.setPackage = false
      }
      this.pagekey = await this.$getPageKey();
    },
    async showBatchSetPackage() {
      if (this.tableChecked.length === 0) {
        this.$message({ message: '请选择要绑定的套餐', type: 'error' })
      } else if ( this.tableChecked.length > 1 ) {
        this.$message({ message: '您只能绑定一个套餐大类', type: 'error' })
      } else if ( this.cardNumberStart == '' && this.cardNumberEnd == '') {
        this.$message({ message: '请输入起始和结束业务号码', type: 'error' })
      } else {
        this.operation.setPackage = true
      }
      this.pagekey = await this.$getPageKey();
    },
    handleSelectionChange(val) {
      this.tableChecked = val
      const length = this.tableChecked.length
      //console.log(`共 ${length} 条`)
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initData()
    },
    toBack() {
      this.$router.back(-1)
    }
  }
}
</script>

<style scoped lang="scss">
.el-container {
  .el-header {
    margin-top: 5vh;
    .el-form-item /deep/ .el-form-item__label {
      text-align: left;
    }
  }
  .el-main {
    .block {
      float: right;
    }
    .el-dropdown-link {
      font-size: 20px;
      cursor: pointer;
    }
  }
}
</style>
